package com.liang.leetcode.string;

import java.util.Arrays;

public class String01_ReverseString {
    public static void main(String[] args) {
        String[] str = {"h", "e", "l", "l", "o"};
        // 将String数组转换为char数组
        char[] charArray = new char[str.length];
        for (int i = 0; i < str.length; i++) {
            charArray[i] = str[i].charAt(0);
        }
        System.out.println(Arrays.toString(charArray));
        reverseString2(charArray);
        // charArray = new StringBuilder(String.valueOf(charArray)).reverse().toString().toCharArray();
        System.out.println(Arrays.toString(charArray));
    }

    /**
     * 反转字符串
     * 用两个指针同时向中间移动，并交换元素
     * @param s 需要反转的字符数组
     */
    public static void reverseString(char[] s) {
        int i = 0, j = s.length - 1;
        while (i <= j) {
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            i++;
            j--;
        }
    }

    /**
     *
     * @param chars
     */
    public static void reverseString2(char[] chars) {
        chars = new StringBuilder(new String(chars)).reverse().toString().toCharArray();
        System.out.println(Arrays.toString(chars));
    }
}
